/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.forms.components;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.easyway.editor.forms.UEditor;
import org.easyway.editor.forms.components.interfaces.IValidity;

public class InputText extends JPanel implements IValidity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3331523944584888846L;

	public String name;

	public boolean isNumber, isFloat;

	public String errorText = "You must write an integer number";

	private JTextField jtext;

	private JLabel label;

	ArrayList<Group> groups = new ArrayList<Group>(1);

	private static Color GREEN = new Color(128, 255, 0);

	public static Icon iconV = new ImageIcon(Thread.currentThread().getClass()
			.getResource("/org/easyway/editor/forms/images/V.gif"));

	public static Icon iconX = new ImageIcon(Thread.currentThread().getClass()
			.getResource("/org/easyway/editor/forms/images/X.gif"));

	boolean validity = false;

	public InputText(String text, String name, boolean isNumber) {
		setLayout(new GridLayout(1, 2));
		this.name = name;
		this.isNumber = isNumber;
		this.isFloat = false;
		add(label = new JLabel(text));
		add(jtext = addListener(new JTextField()));
		UEditor.getLeftPanel().add(this);
		Group.AllObjects.add(this);
		validity = checkValidity();
		validate();
	}

	public InputText(String text, String name, boolean isNumber, boolean isFloat) {
		setLayout(new GridLayout(1, 2));
		this.name = name;
		this.isNumber = isNumber;
		this.isFloat = isFloat;
		add(label = new JLabel(text));
		add(jtext = addListener(new JTextField()));
		UEditor.getLeftPanel().add(this);
		Group.AllObjects.add(this);
		checkValidity();
		validate();
	}

	protected JTextField addListener(final JTextField textField) {
		textField.addCaretListener(new javax.swing.event.CaretListener() {
			public void caretUpdate(javax.swing.event.CaretEvent e) {
				checkValidity();
			}
		});
		return textField;
	}

	public void setValid(boolean value) {
		if (value) {
			jtext.setBackground(GREEN);
			label.setIcon(iconV);
		} else {
			jtext.setBackground(Color.PINK);
			label.setIcon(iconX);
		}
		validity = value;
		for (Group g : groups) {
			g.updateValidity(value);
		}
	}

	public boolean getValidity() {
		return validity;
	}

	public boolean checkValidity() {
		if (isNumber) {
			if (isFloat) {
				try {
					Float.parseFloat(jtext.getText());
				} catch (NumberFormatException e) {
					setValid(false);
					return false;
				}
				setValid(true);
				return true;
			} else {
				try {
					Integer.parseInt(jtext.getText());
				} catch (NumberFormatException e) {
					setValid(false);
					return false;
				}
				setValid(true);
				return true;
			}
		}
		setValid(true);
		return true;
	}

	public int getInt() {
		if (!isNumber)
			return 0;
		if (jtext.getText().equals("")) {
			return 0;
		}
		try {
			return Integer.parseInt(jtext.getText());
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public float getFloat() {
		if (!isFloat)
			return 0;
		if (jtext.getText().equals("")) {
			return 0;
		}
		try {
			return Float.parseFloat(jtext.getText());
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public String getValue() {
		return jtext.getText();
	}

	public void setValue(String text) {
		jtext.setText(text);
		checkValidity();
	}
	
	public String getName() {
		return name;
	}
	
	public ArrayList<Group> getGroups() {
		return groups;
	}
}
